package com.g7.figmaapi.common.result;

import com.g7.figmaapi.common.exception.Errors;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.Serializable;

/**
 * @author: taoxiangqian
 * @explain: 全局返回方法
 * @since: 2023/10/26 10:26
 */
public class WebResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private String code;
    private String msg = "";
    private String exception = "";
    private T data;
    private Long timestamp = System.currentTimeMillis();

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getException() {
        return this.exception;
    }

    public void setException(String exception) {
        this.exception = exception;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Long getTimestamp() {
        return this.timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public static <T> WebResult<T> success() {
        WebResult<T> entity = new WebResult();
        entity.setCode(Errors.SUCCESS.code);
        entity.setMsg(Errors.SUCCESS.msg);
        entity.setException(Errors.SUCCESS.exception);
        return entity;
    }

    public static <T> WebResult<T> success(T data) {
        WebResult<T> entity = new WebResult();
        entity.setCode(Errors.SUCCESS.code);
        entity.setMsg(Errors.SUCCESS.msg);
        entity.setException(Errors.SUCCESS.exception);
        entity.setData(data);
        return entity;
    }

    public static <T> WebResult<T> success(String msg, T data) {
        WebResult<T> entity = new WebResult();
        entity.setCode(Errors.SUCCESS.code);
        entity.setData(data);
        entity.setMsg(msg);
        return entity;
    }

    public static <T> WebResult<T> fail(Errors errors) {
        WebResult<T> entity = new WebResult();
        entity.setCode(errors.code);
        entity.setMsg(errors.msg);
        entity.setException(errors.exception);
        return entity;
    }

    public static <T> WebResult<T> fail(Errors errors, T data) {
        WebResult<T> entity = new WebResult();
        entity.setCode(errors.code);
        entity.setMsg(errors.msg);
        entity.setException(errors.exception);
        entity.setData(data);
        return entity;
    }

    public static <T> WebResult<T> fail(String code, T data) {
        WebResult<T> entity = new WebResult();
        entity.setCode(code);
        entity.setData(data);
        return entity;
    }

    public static <T> WebResult<T> fail(String code, String exception, String msg, T data) {
        WebResult<T> entity = new WebResult();
        entity.setCode(code);
        entity.setException(exception);
        entity.setMsg(msg);
        entity.setData(data);
        return entity;
    }

    public WebResult() {
    }

    public WebResult(String code, String msg, String exception) {
        this.code = code;
        this.msg = msg;
        this.exception = exception;
    }

    public WebResult(Errors errors) {
        this.code = errors.code;
        this.msg = errors.msg;
        this.exception = errors.exception;
    }

    public WebResult(Errors errors, T data) {
        this.code = errors.code;
        this.msg = errors.msg;
        this.exception = errors.exception;
        this.data = data;
    }

    public String toString() {
        return JSON.toJSONString(this, new SerializerFeature[]{SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteDateUseDateFormat});
    }
}
